package cn.cstech.aijia.wxapi;

import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelmsg.SendAuth;
import com.tencent.mm.sdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.sdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.sdk.modelmsg.WXMediaMessage;

import cn.cstech.aijia.R;
import cn.cstech.aijia.sns.Constants;
import cn.cstech.aijia.sns.SnsErrorCode;
import cn.cstech.aijia.sns.weixin.BaseWXEntryActivity;
import cn.cstech.aijia.sns.weixin.GetFromWXActivity;
import cn.cstech.aijia.sns.weixin.ShowFromWXActivity;
import cn.cstech.aijia.ui.base.utils.LocalUtils;
import cn.cstech.aijia.ui.base.utils.SPUtils;


/**
 * 微信请求和收到响应
 *
 * @author evan zhanghui451@jk.cn 14-9-16
 * @version 0.1
 */
public class WXEntryActivity extends BaseWXEntryActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
    
    @Override
	public void onReq(BaseReq req) {
		switch (req.getType()) {
		case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
			goToGetMsg();		
			break;
		case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
			goToShowMsg((ShowMessageFromWX.Req) req);
			break;
		default:
			break;
		}
	}

	@Override
	public void onResp(BaseResp baseResp) {
    	if(baseResp instanceof SendAuth.Resp){
    		super.onResp(baseResp);
    		return;
    	}

		switch (baseResp.errCode) {
		case BaseResp.ErrCode.ERR_OK:
			LocalUtils.showToast(this, R.string.toast_share_success);
			sendShareResultBroadcast(SnsErrorCode.SUCCESS.ordinal(), null);
//			LocalUtils.sendAppendAction(this);
			break;
		case BaseResp.ErrCode.ERR_USER_CANCEL:
			LocalUtils.showToast(this, R.string.toast_share_cancel);
			sendShareResultBroadcast(SnsErrorCode.CANCEL.ordinal(), null);
//			LocalUtils.sendAppendAction(this);
			break;
		default:
			LocalUtils.showToast(this, baseResp.errStr);
			sendShareResultBroadcast(SnsErrorCode.FAILED.ordinal(),baseResp.errStr);
			break;
		}
		finish();
	}
	
	private void goToGetMsg() {
		Intent intent = new Intent(this, GetFromWXActivity.class);
		intent.putExtras(getIntent());
		startActivity(intent);
		finish();
	}
	
	private void goToShowMsg(ShowMessageFromWX.Req showReq) {
		WXMediaMessage wxMsg = showReq.message;
		WXAppExtendObject obj = (WXAppExtendObject) wxMsg.mediaObject;
		
		StringBuffer msg = new StringBuffer(); 
		msg.append("description: ");
		msg.append(wxMsg.description);
		msg.append("\n");
		msg.append("extInfo: ");
		msg.append(obj.extInfo);
		msg.append("\n");
		msg.append("filePath: ");
		msg.append(obj.filePath);
		
		Intent intent = new Intent(this, ShowFromWXActivity.class);
		intent.putExtra(Constants.ShowMsgActivity.STitle, wxMsg.title);
		intent.putExtra(Constants.ShowMsgActivity.SMessage, msg.toString());
		intent.putExtra(Constants.ShowMsgActivity.BAThumbData, wxMsg.thumbData);
		startActivity(intent);
		finish();
	}
	
	private void sendShareResultBroadcast(int errorCode,String errorMsg){
		Intent intent = new Intent(SPUtils.ACTION_SHARE_RESULT);
		intent.putExtra(SPUtils.EXTRA_CODE, errorCode);
		intent.putExtra(SPUtils.EXTRA_MESSAGE, errorMsg);
		sendBroadcast(intent);
	}
}